package com.api.demo


import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mobvoi.android.gesture.GestureType
import com.mobvoi.android.gesture.MobvoiGestureClient


class DemoActivity : AppCompatActivity(), SensorEventListener {

    private final val TAG: String = "DemoActivity"
    private lateinit var sensorManager: SensorManager
    private var heartRateSensor: Sensor? = null
    private var stepCounterSensor: Sensor? = null
    private var lightSensor: Sensor? = null
    private lateinit var client: MobvoiGestureClient
    private lateinit var rootView: View
    private var mutableMap = mutableMapOf<Int, String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo)
        rootView = findViewById(R.id.rootView)
        sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
        heartRateSensor = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE)
        stepCounterSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
        lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
        if (lightSensor == null) {
            Log.e(TAG, "onCreate: lightSensor is null")
        } else {
            Log.e(TAG, "onCreate: lightSensor is not null")
        }

    }


    override fun onResume() {
        super.onResume()
        sensorManager.registerListener(this@DemoActivity, heartRateSensor, SensorManager.SENSOR_DELAY_NORMAL)
        sensorManager.registerListener(this@DemoActivity, stepCounterSensor, SensorManager.SENSOR_DELAY_NORMAL)
        client = MobvoiGestureClient.getInstance(GestureType.GROUP_TURN_WRIST)
        client.register(
            this@DemoActivity
        ) { type ->
            Log.d("callback", "onGestureDetected type: $type")
            rootView.post {
                val s = when (type) {
                    GestureType.TYPE_TWICE_TURN_WRIST -> {
                        "两次翻腕"
                    }

                    GestureType.TYPE_TURN_WRIST_UP -> {
                        "向上翻页"
                    }

                    GestureType.TYPE_TURN_WRIST_DOWN -> {
                        "向下翻页"
                    }

                    else -> {
                        "未知"
                    }
                }
                Toast.makeText(applicationContext, "onGestureDetected $s", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        sensorManager.unregisterListener(this@DemoActivity)
        client.unregister(this@DemoActivity)
    }

    override fun onSensorChanged(event: SensorEvent) {
        Log.e(TAG, "onSensorChanged: ${event.accuracy}, ${event.values[0]}")
        if (event.sensor == heartRateSensor) {
            mutableMap[0] = "Heatbeat: ${event.values[0]}"
        } else if (event.sensor == stepCounterSensor) {
            mutableMap[1] = "StepSensor: ${event.values[0]}"
        }
        var lines = ""
        for (mutableEntry in mutableMap) {
            lines += mutableEntry.value + "\r\n"
        }
        rootView.findViewById<TextView>(R.id.text_content).text = lines
    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        Log.e(TAG, "onAccuracyChanged: $accuracy")
    }
}